package com.veo.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;

//除去配置项目中，对象的依赖每次new一个造成IOC容器混乱，每次都从IOC容器拿为单实例的
@Configuration(proxyBeanMethods = true)
public class MyServletConfig {

    //向容器中注入servlet
    @Bean
    public ServletRegistrationBean<MyServlet> myServlet(){
        return new ServletRegistrationBean(new MyServlet(),"/my","/my1");
    }

    @Bean
    public FilterRegistrationBean myFilter(){
        //拦截servlet中的路径
//        return new FilterRegistrationBean(new MyFilter(),myServlet());

        FilterRegistrationBean<Filter> filter = new FilterRegistrationBean<>();
        filter.setFilter(new MyFilter());
        filter.setUrlPatterns(Arrays.asList("/css/*","/images/*"));
        return filter;
    }

    @Bean
    public ServletListenerRegistrationBean myListener(){
        return new ServletListenerRegistrationBean(new MyListener());
    }


}
